//
//  FeatureViewController.m
//  iBook
//
//  Created by wangjing on 14/12/3.
//  Copyright (c) 2014年 wikiflyer. All rights reserved.
//

#import "FeatureViewController.h"
#import "FeatureTableViewCell.h"
#import "AllBookViewController.h"
#import "ShuJiXiangQing_ViewController.h"
#import "JingXuan_FenLei_ViewController.h"
#import "CommonFunc.h"
#import <QuartzCore/QuartzCore.h>
#import "ShuJiXiangQing_ViewController.h"
#import "JingXuan_FenLei_ViewController.h"
#import "WeiDuZhanShi_ViewController.h"

#define MRScreenHeight     CGRectGetHeight([UIScreen mainScreen].applicationFrame)
#define MRScreenWidth      CGRectGetWidth([UIScreen mainScreen].applicationFrame)


@interface FeatureViewController ()<FeatureScrollCellDelegate,FeatureTableCellDelegate>


@end

@implementation FeatureViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.navigationItem.title = @"图书";

    [self initTableView];
    NSLog(@"%f    %f   ",self.view.frame.size.height,self.view.frame.size.width);
    
    
    [self goLineFenLei];
    [self goLineBanner];
    
    [self goLineWeiDu];
    
    
}
//初始化TableView
-(void)initTableView{
    _myTableView=[[UITableView alloc] initWithFrame:CGRectMake(0, 64, MRScreenWidth, MRScreenHeight-120-50-44+120) style:UITableViewStylePlain];
    _myTableView.dataSource = self;
    _myTableView.delegate = self;
    [self.view addSubview:_myTableView];
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor clearColor];
    [_myTableView setTableFooterView:view];
    [_myTableView setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:_myTableView];
}

//初始化Top ScrollView
-(void)initTopScrollView:(NSMutableArray *)theArray{
//    CGFloat topicW = self.topView.frame.size.width;
//    CGFloat topicH = self.topView.frame.size.height;
    //实例化
    _Topic = [[JCTopic alloc]initWithFrame:CGRectMake(0, 0, MRScreenWidth, 106)];
    //代理
    _Topic.JCdelegate = self;
    //创建数据
    NSMutableArray * tempArray = [[NSMutableArray alloc]init];
    for (int a=0; a<theArray.count; a++) {
        NSData* dataImage = [CommonFunc dataWithBase64EncodedString:[NSString stringWithFormat:@"%@",[theArray[a] objectForKey:@"pic"]]];
        UIImage * image=[UIImage imageWithData:dataImage];
        [tempArray addObject:[NSDictionary dictionaryWithObjects:@[image ,@"",@YES] forKeys:@[@"pic",@"title",@"isLoc"]]];
    }
    
    
    //本地图片
    //***********************//
    //key pic = 本地 UIImage
    //key title = 显示的标题 NSString
    //key isLoc = 是否本地图片 Bool
    //***********************//
//    UIImage * image = [UIImage imageNamed:@"1.jpg"];
//    [tempArray addObject:[NSDictionary dictionaryWithObjects:@[image ,@"PIC1",@YES] forKeys:@[@"pic",@"title",@"isLoc"]]];
//    UIImage * image2 = [UIImage imageNamed:@"2.jpg"];
//    [tempArray addObject:[NSDictionary dictionaryWithObjects:@[image2 ,@"PIC2",@YES] forKeys:@[@"pic",@"title",@"isLoc"]]];
    //网络图片
    //***********************//
    //key pic = 地址 NSString
    //key title = 显示的标题 NSString
    //key isLoc = 是否本地图片 Bool
    //key placeholderImage = 网络图片加载失败时显示的图片 UIImage
    //***********************//
//    [tempArray addObject:[NSDictionary dictionaryWithObjects:@[@"http://163.54114.com/upimg/allimg/120619/5-120619112512.jpg" ,@"PIC3",@NO] forKeys:@[@"pic",@"title",@"isLoc"]]];
//    //网络图片加载失败
//    UIImage * PlaceholderImage = [UIImage imageNamed:@"3.jpg"];
//    [tempArray addObject:[NSDictionary dictionaryWithObjects:@[@"http://s.doyo.cn/img/52/cf/91779e9e784d2c000003.jpg" ,@"PIC4",@NO,PlaceholderImage] forKeys:@[@"pic",@"title",@"isLoc",@"placeholderImage"]]];
    
    //加入数据
    _Topic.pics = tempArray;
    //更新
    [_Topic upDate];
//    [self.view addSubview:_Topic];
}


#pragma mark - JCTopic的
-(void)didClick:(id)data{
    NSLog(@"%@",(NSArray*)data);
}
-(void)currentPage:(int)page total:(NSUInteger)total{
    
}
///////////////////////////////////////////////////
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _arrayFenLei.count+1+1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
//    FeatureTableViewCell *cell = [FeatureTableViewCell CellWithTableView:tableView];
//    [cell initData:indexPath];
//    cell.cellDelegate = self;
//    cell.showDelegate = self;
    NSString * str1=@"123";
    NSString * str2=@"1234";
    NSString * str3=@"12345";
    JingXuanShouYe_TableViewCell * cell=[tableView dequeueReusableCellWithIdentifier:str1];
    UITableViewCell * cell2=[tableView dequeueReusableCellWithIdentifier:str2];
    UITableViewCell * cell3=[tableView dequeueReusableCellWithIdentifier:str3];
    if (!cell) {
        cell=[JingXuanShouYe_TableViewCell homeCellCellOwner:self];
    }
    if (!cell2) {
        cell2=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str2];
    }
    if (!cell3) {
        cell3=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str3];
    }
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    cell2.selectionStyle=UITableViewCellSelectionStyleNone;
    cell3.selectionStyle=UITableViewCellSelectionStyleNone;
    if (indexPath.row==0) {
        [cell2 addSubview:_Topic];
        return cell2;
    }else if (indexPath.row==(_arrayFenLei.count+1)){
        if (_arrayWeiDu.count>0) {
            [cell3 addSubview:_viewWeiDu];
        }
        return cell3;
    }else{
        if (_arrayShuJi.count>0) {
            [cell initWithSome:_arrayShuJi[indexPath.row-1] thetitle:_arrayFenLei[indexPath.row-1]];
            cell.delegate=self;
        }
        return cell;
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //取消选中状态
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row==0) {
        return 106;
    }else if (indexPath.row==(_arrayFenLei.count+1)){
        if (_arrayWeiDu.count>0) {
            return 126;
        }else{
            return 0;
        }
    }else{
        return 245;
    }
}


#pragma mark - FeatureScrollCellDelegate
- (void)onClickedImageWithTag:(int)index
{
    NSLog(@"点击了图片%d",index);
    ShuJiXiangQing_ViewController * shujiV=[[ShuJiXiangQing_ViewController alloc] init];
    [self.navigationController pushViewController:shujiV animated:YES];
}
#pragma mark - FeatureTableCellDelegate
-(void)showBtnClick:(int)tag{
    NSLog(@"点击了 %d",tag);
    JingXuan_FenLei_ViewController *allVC = [[JingXuan_FenLei_ViewController alloc]init];
    allVC.title = @"简体中文";
    [allVC setHidesBottomBarWhenPushed:YES];
    [self.navigationController pushViewController:allVC animated:YES];
}
//--------------------------------

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

//存在分类和纬度 （分类 － － 中文／英文等）  （纬度 － － 不带文字的展示按钮）

#pragma mark - 数据请求(Json-Post)
#pragma mark - 网络请求 - - 分类
-(void)goLineFenLei{
    [self getFenLei:@"http://www.xdoc.cn/json/LoadBrickInfo.aspx"andDictionary:nil];
}
-(void)getFenLei:(NSString *)url andDictionary:(NSDictionary *)yourDictionary{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/plain",@"text/html",nil];
    [manager GET:url parameters:yourDictionary success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [self initWithDataSFenLei:responseObject];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
}
-(void)initWithDataSFenLei:(NSDictionary *)theDic{
//    NSLog(@" - - - all = %@",theDic);
    _arrayFenLei=[[NSMutableArray alloc] init];
    [_arrayFenLei addObjectsFromArray:[theDic objectForKey:@"his"]];
    [_myTableView reloadData];
    _arrayShuJi=[[NSMutableArray alloc] init];
    for (int a=0; a<_arrayFenLei.count; a++) {
        NSMutableArray * array=[[NSMutableArray alloc] init];
        [_arrayShuJi addObject:array];
        [self goLineShuJi:[_arrayFenLei[a] objectForKey:@"hid"] theIndex:a];
    }
}
#pragma mark - 网络请求 - - 分类下所有的书籍
-(void)goLineShuJi:(NSString *)theType theIndex:(int)theIndex{
    NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
    [dic setObject:theType forKey:@"hid"];
    [self getShuJi:@"http://www.xdoc.cn/json/LoadOliveInfo.aspx?"andDictionary:dic theIndex:theIndex];
}
-(void)getShuJi:(NSString *)url andDictionary:(NSDictionary *)yourDictionary theIndex:(int)theIndex{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/plain",@"text/html",nil];
    [manager GET:url parameters:yourDictionary success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [self initWithDataSShuJi:responseObject theIndex:theIndex];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
}
-(void)initWithDataSShuJi:(NSDictionary *)theDic theIndex:(int)theIndex{
//    NSLog(@" - - - all = %@",theDic);
    NSMutableArray * array=[[NSMutableArray alloc] initWithArray:[theDic objectForKey:@"ois"]];
    _arrayShuJi[theIndex]=array;
    [_myTableView reloadData];
}
#pragma mark - 网络请求 - - 上方滚动图片
-(void)goLineBanner{
    [self getBanner:@"http://www.xdoc.cn/json/LoadAboveInfo.aspx"andDictionary:nil];
}
-(void)getBanner:(NSString *)url andDictionary:(NSDictionary *)yourDictionary{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/plain",@"text/html",nil];
    [manager GET:url parameters:yourDictionary success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [self initWithDataSBanner:responseObject];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
}
-(void)initWithDataSBanner:(NSDictionary *)theDic{
//    NSLog(@" - - - all = %@",theDic);
    NSMutableArray * theArray=[[NSMutableArray alloc] init];
    [theArray addObjectsFromArray:[theDic objectForKey:@"ois"]];
    [self initTopScrollView:theArray];
    [_myTableView reloadData];
}


#pragma mark - 网络请求 - - 纬度
-(void)goLineWeiDu{
    //    NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
    //    [dic setObject:@"1" forKey:@"hid"];
    [self getWeiDu:@"http://www.xdoc.cn/json/LoadAlbumInfo.aspx"andDictionary:nil];
}
-(void)getWeiDu:(NSString *)url andDictionary:(NSDictionary *)yourDictionary{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/plain",@"text/html",nil];
    [manager GET:url parameters:yourDictionary success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [self initWithDataSWeiDu:responseObject];
        //        [self.view.window showHUDWithText:@"" Type:ShowDismiss Enabled:YES];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
}
-(void)initWithDataSWeiDu:(NSDictionary *)theDic{
//    NSLog(@" - - - all纬度 = %@",theDic);
    _arrayWeiDu=[[NSMutableArray alloc] init];
    [_arrayWeiDu addObjectsFromArray:[theDic objectForKey:@"ais"]];
    [self initWithWeiDuScrollV:_arrayWeiDu theTitle:[theDic objectForKey:@"dsc"]];
    [_myTableView reloadData];
}
-(void)initWithWeiDuScrollV:(NSMutableArray *)theArray theTitle:(NSString *)theTitle{
    
    _viewWeiDu=[[UIView alloc] initWithFrame:CGRectMake(0, 0, MRScreenWidth, 126)];
    
    UILabel * label=[[UILabel alloc] initWithFrame:CGRectMake(20, 9, 200, 21)];
    label.text=theTitle;
    
    UIScrollView * scrollVWeiDu = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 30, MRScreenWidth, 96)];
    scrollVWeiDu.showsHorizontalScrollIndicator = NO;
    scrollVWeiDu.contentSize=CGSizeMake(MRScreenWidth*2, 96);
    for (int a=0; a<theArray.count; a++) {
        NSData* dataImage = [CommonFunc dataWithBase64EncodedString:[NSString stringWithFormat:@"%@",[theArray[a] objectForKey:@"pic"]]];
        UIButton * btn=[UIButton buttonWithType:UIButtonTypeCustom];
        btn.layer.cornerRadius=8.0;
        btn.tag=1000+a;
        btn.layer.masksToBounds = YES;
        btn.frame=CGRectMake(12+(155*a), 13, 143, 70);
        [btn setBackgroundImage:[UIImage imageWithData:dataImage] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(goBtnWeiDu:) forControlEvents:UIControlEventTouchUpInside];
        [scrollVWeiDu addSubview:btn];
    }
    int theWidth = 12+(155*(theArray.count - 1))+143+20;
    scrollVWeiDu.contentSize=CGSizeMake(theWidth, 96);
    
    [_viewWeiDu addSubview:scrollVWeiDu];
    [_viewWeiDu addSubview:label];
}

#pragma mark - btn
-(void)goBtnWeiDu:(UIButton *)sender{
//    NSLog(@"纬度 - %@",[_arrayWeiDu[sender.tag-1000] objectForKey:@"aid"]);
    WeiDuZhanShi_ViewController * weiduV=[[WeiDuZhanShi_ViewController alloc] init];
    weiduV.strWeiDuID=[_arrayWeiDu[sender.tag-1000] objectForKey:@"aid"];
    [self.navigationController pushViewController:weiduV animated:YES];
}


#pragma mark - JingXuanShouYe_TableViewCell_Detegate
-(void)JingXuanShouYe_TableViewCell_Detegate:(JingXuanShouYe_TableViewCell *)theCell theID:(NSString *)theID{
    NSLog(@"选中的id %@",theID);
    ShuJiXiangQing_ViewController * shujiV=[[ShuJiXiangQing_ViewController alloc] init];
    shujiV.strIDJieShou=theID;
    [self.navigationController pushViewController:shujiV animated:YES];
}

-(void)JingXuanShouYe_TableViewCell_DetegateQuanBu:(JingXuanShouYe_TableViewCell *)theCell theID:(NSString *)theID{
    NSLog(@"全部显示");
    JingXuan_FenLei_ViewController * jingV=[[JingXuan_FenLei_ViewController alloc] init];
    jingV.strIDJieShou=theID;
    [self.navigationController pushViewController:jingV animated:YES];
}


@end
